from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtWidgets import (QDialog, QVBoxLayout, QLabel, QPushButton,
                             QHBoxLayout, QTextEdit, QApplication, QSizePolicy)

from qrcode_util import QRCodeGenerator


class ShareDialog(QDialog):
    def __init__(self, share_url, parent=None):
        super().__init__(parent)
        self.share_url = share_url
        self.setup_ui()
        self.setWindowTitle("共享考题文件")
        self.setMinimumSize(400, 500)

    def setup_ui(self):
        layout = QVBoxLayout()

        # 标题
        title_label = QLabel("扫描二维码获取共享文件")
        title_label.setAlignment(Qt.AlignCenter)
        title_label.setStyleSheet("font-size: 16px; font-weight: bold;")
        layout.addWidget(title_label)

        # 二维码显示区域
        self.qrcode_label = QLabel()
        self.qrcode_label.setAlignment(Qt.AlignCenter)
        self.qrcode_label.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)

        # 生成并显示二维码
        qr_pixmap = QRCodeGenerator.generate_qrcode(self.share_url, 300)
        self.qrcode_label.setPixmap(qr_pixmap)
        layout.addWidget(self.qrcode_label)

        # 下载提示
        hint_label = QLabel("或复制下方链接到浏览器下载:")
        hint_label.setAlignment(Qt.AlignCenter)
        layout.addWidget(hint_label)

        # URL显示框
        self.url_edit = QTextEdit()
        self.url_edit.setPlainText(self.share_url)
        self.url_edit.setMaximumHeight(60)
        self.url_edit.setReadOnly(True)
        self.url_edit.setStyleSheet("""
            QTextEdit {
                border: 1px solid #ccc;
                border-radius: 4px;
                padding: 5px;
                font-family: monospace;
                background-color: #f8f9fa;
            }
        """)
        layout.addWidget(self.url_edit)

        # 按钮区域
        button_layout = QHBoxLayout()

        self.copy_btn = QPushButton("复制链接")
        self.copy_btn.setIcon(QApplication.style().standardIcon(QApplication.style().SP_DialogSaveButton))
        self.copy_btn.clicked.connect(self.copy_to_clipboard)
        self.copy_btn.setStyleSheet("padding: 5px 10px;")

        self.open_btn = QPushButton("打开链接")
        self.open_btn.setIcon(QApplication.style().standardIcon(QApplication.style().SP_DialogOpenButton))
        self.open_btn.clicked.connect(self.open_in_browser)
        self.open_btn.setStyleSheet("padding: 5px 10px;")

        button_layout.addWidget(self.copy_btn)
        button_layout.addWidget(self.open_btn)
        layout.addLayout(button_layout)

        self.setLayout(layout)

    def copy_to_clipboard(self):
        """复制URL到剪贴板"""
        clipboard = QApplication.clipboard()
        clipboard.setText(self.share_url)

        # 显示复制成功反馈
        self.copy_btn.setText("已复制!")
        QTimer.singleShot(2000, lambda: self.copy_btn.setText("复制链接"))

    def open_in_browser(self):
        """在默认浏览器中打开链接"""
        import webbrowser
        webbrowser.open(self.share_url)
